package com.work.lxykeeper.view;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.work.lxykeeper.R;
import com.work.lxykeeper.cache.Ckey;
import com.work.lxykeeper.cache.SPutils;
import com.work.lxykeeper.httpretrofit.Url;
import com.work.lxykeeper.module.main.WebViewActivity;
import com.work.lxykeeper.utils.UIUtils;


/**
 * Created by WZ on 2021/6/23.
 * description:
 */
public class PrivacyPolicyDialog {
    public static void startDialog(final Activity context, CallbackAgree callbackAgree) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.show();
        alertDialog.setCancelable(false);
        Window window = alertDialog.getWindow();
        if (window != null) {
            window.setContentView(R.layout.dialog_initmate);
            window.setGravity(Gravity.CENTER);

            TextView tvContent = window.findViewById(R.id.tv_content);
            TextView tvCancel = window.findViewById(R.id.tv_cancel);
            TextView tvAgree = window.findViewById(R.id.tv_agree);
            String str = "\t\t\t\t本应用尊重并保护所有使用服务用户的个人隐私权。" +
                    "为了给您提供更准确、更有个性化的服务，本应用会按照本隐私权政策的规定使用和披露您的个人信息。" +
                    "可阅读《隐私政策》和《软件许可及服务协议》";

            SpannableStringBuilder ssb = new SpannableStringBuilder();
            ssb.append(str);
            final int start = str.indexOf("《");//第一个出现的位置
            ssb.setSpan(new ClickableSpan() {
                @Override
                public void onClick(@NonNull View widget) {
                    //《隐私政策》
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("url", Url.PrivacyPolicy);
                    intent.putExtra("title", "隐私政策");
                    UIUtils.startActivity(intent);
                }

                @Override
                public void updateDrawState(@NonNull TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(context.getResources().getColor(R.color.blue));
                    ds.setUnderlineText(false);
                }
            }, start, start + 6, 0);

            int end = str.lastIndexOf("《");
            ssb.setSpan(new ClickableSpan() {
                @Override
                public void onClick(@NonNull View widget) {
                    //《软件许可及服务协议》
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("url", Url.UserAgreement);
                    intent.putExtra("title", "软件许可及服务协议");
                    UIUtils.startActivity(intent);
                }

                @Override
                public void updateDrawState(@NonNull TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(context.getResources().getColor(R.color.blue));
                    ds.setUnderlineText(false);
                }
            }, end, end + 11, 0);

            tvContent.setMovementMethod(LinkMovementMethod.getInstance());
            tvContent.setText(ssb, TextView.BufferType.SPANNABLE);


            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertDialog.cancel();
                    context.finish();
                    if (callbackAgree != null) {
                        callbackAgree.unagreen();
                    }
                }
            });

            tvAgree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SPutils.put(Ckey.ISFIRSTPRIVACY, "no");
                    alertDialog.cancel();
                    if (callbackAgree != null) {
                        callbackAgree.agreen();
                    }
                }
            });
        }

    }

    public interface CallbackAgree {
        void agreen();

        void unagreen();
    }
}
